package com.books.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.books.mapper.BookInfoMapper;
import com.books.models.tb_books;
import com.books.services.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 刘志伟
 * @create 2022/9/21 13:47
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookInfoMapper, tb_books> implements BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<tb_books> getBooks(String author, String name, String publisher) {

        LambdaQueryWrapper<tb_books> queryWrapper = Wrappers.<tb_books>lambdaQuery()
                .eq(tb_books::getAuthor, author)
                .eq(tb_books::getName, name)
                .eq(tb_books::getPublisher, publisher);
        List<tb_books> result = bookInfoMapper.selectList(queryWrapper);
        if (result.size() == 0){
            tb_books item = bookInfoMapper.selectById(1);
            result.add(item);
        }
        return result;
    }
}
